package com.techwells.heartfelt.utils;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;

import org.slf4j.Logger;

public class LoggerFactory {

    private static final Logger logger = 
            org.slf4j.LoggerFactory.getLogger( LoggerFactory.class );

    private static final String LOGBACK_FILE_NAME = "logback.xml";
    
    @SuppressWarnings("unused")
	private static final String BASE_EVN_PATH="";
    
    public static void resetLogContext() {
        LoggerContext context = 
                (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory();
        try {
            JoranConfigurator configurator = new JoranConfigurator();
            configurator.setContext(context);

            String path =  LOGBACK_FILE_NAME;

            logger.warn( "reset log context with file: " + path );
            
            context.reset();
            
            configurator.doConfigure( Thread.currentThread()
                .getContextClassLoader()
                .getResourceAsStream( path ) );
            
            logger.warn( "reset log context completed!" );

        } catch ( JoranException ex ) {
            logger.error( "reset log context exception occurred!!", ex );
            throw new RuntimeException( ex );
        }

        if ( logger.isInfoEnabled() ) {
            StatusPrinter.printInCaseOfErrorsOrWarnings( context );
        }
    }

}
